package mvc.handler;

import com.github.pagehelper.PageInfo;
import entity.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import service.api.RoleService;
import util.ResultEntity;

import static util.ResultEntity.*;

@RestController
public class RoleController {

    @Autowired
    private RoleService RoleService;

    @ResponseBody
    @RequestMapping("role/save.json")
    public ResultEntity<Role> saveRole(Role  role) {
        RoleService.saveRole(role);
        return ResultEntity.successWithoutData();
    }



    @ResponseBody
    @RequestMapping("/role/get/page/info.json")
    public ResultEntity<PageInfo<Role>> getPageInfo(
            @RequestParam(value="pageNum", defaultValue="1") Integer pageNum,
            @RequestParam(value="pageSize", defaultValue="5") Integer pageSize,
            @RequestParam(value="keyword", defaultValue="") String keyword
    ) {

        // 调用Service方法获取分页数据
        PageInfo<Role> pageInfo = RoleService.getPageInfo(pageNum, pageSize, keyword);

        // 封装到ResultEntity对象中返回（如果上面的操作抛出异常，交给异常映射机制处理）
        return ResultEntity.successWithData(pageInfo);
    }
}
